/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

package org.erossi.pigest.web.tipodocumento;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.erossi.pigest.model.documento.TipoDocumento;
import org.springframework.web.servlet.view.document.AbstractExcelView;

/**
 * 'ListaTipiDocumentoHSSFView' class: classe che costituisce la vista per la versione HSSF
 *   della lista dei tipi di documento.
 *
 * @author erossi
 * @version Sep 24, 2008
 */
public class ListaTipiDocumentoHSSFView extends AbstractExcelView {

  @Override
  @SuppressWarnings("unchecked")
  protected void buildExcelDocument(Map model, HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception {

    HSSFCellStyle dateCellStyle = workbook.createCellStyle();
    dateCellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
    
    HSSFCellStyle currencyCellStyle = workbook.createCellStyle();
    currencyCellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("#,##0.00"));
    
    HSSFSheet sheet_0 = workbook.createSheet(this.getApplicationContext().getMessage("bean.document_type", null, request.getLocale()));

    HSSFRow row_0 = sheet_0.createRow(0);

    HSSFCell cell_r0c0 = row_0.createCell((short) 0);
    cell_r0c0.setCellValue(this.getApplicationContext().getMessage("bean.document_type.id", null, request.getLocale()));
    HSSFCell cell_r0c1 = row_0.createCell((short) 1);
    cell_r0c1.setCellValue(this.getApplicationContext().getMessage("bean.document_type.version", null, request.getLocale()));  
    HSSFCell cell_r0c2 = row_0.createCell((short) 2);
    cell_r0c2.setCellValue(this.getApplicationContext().getMessage("bean.document_type.name", null, request.getLocale()));
    HSSFCell cell_r0c3 = row_0.createCell((short) 3);
    cell_r0c3.setCellValue(this.getApplicationContext().getMessage("bean.document_type.activeCycle", null, request.getLocale()));
    HSSFCell cell_r0c4 = row_0.createCell((short) 4);
    cell_r0c4.setCellValue(this.getApplicationContext().getMessage("bean.document_type.accountEntry", null, request.getLocale()));
    HSSFCell cell_r0c5 = row_0.createCell((short) 5);
    cell_r0c5.setCellValue(this.getApplicationContext().getMessage("bean.document_type.warehouseEntry", null, request.getLocale()));
    
    List<TipoDocumento> tipiDocumento = (List<TipoDocumento>) model.get("listaTipiDocumento");
    
    if (tipiDocumento != null) {
    
      for (int i = 0; i < tipiDocumento.size(); i++) {
        
        TipoDocumento tipoDocumento_i = tipiDocumento.get(i);
          
        HSSFRow row_k = sheet_0.createRow(sheet_0.getLastRowNum() + 1);
          
        HSSFCell cell_rKc0 = row_k.createCell((short) 0);
        Long id = tipoDocumento_i.getId();
        if (id != null) {cell_rKc0.setCellValue(id);}
          
        HSSFCell cell_rKc1 = row_k.createCell((short) 1);
        Long versione = tipoDocumento_i.getVersion();
        if (versione != null) {cell_rKc1.setCellValue(versione);}
        
        HSSFCell cell_rKc2 = row_k.createCell((short) 2);
        String nome = tipoDocumento_i.getNome();
        if (nome != null) {cell_rKc2.setCellValue(nome);}
        
        HSSFCell cell_rKc3 = row_k.createCell((short) 3);
        Boolean cicloAttivo = tipoDocumento_i.getCicloAttivo();
        if (cicloAttivo != null) {
          if (cicloAttivo) {
            cell_rKc3.setCellValue(this.getApplicationContext().getMessage("general.enabled", null, request.getLocale()));
          } else {
            cell_rKc3.setCellValue(this.getApplicationContext().getMessage("general.disabled", null, request.getLocale()));
          }
        }
        
        HSSFCell cell_rKc4 = row_k.createCell((short) 4);
        Boolean movimentoContabile = tipoDocumento_i.getMovimentoContabile();
        if (movimentoContabile != null) {
          if (movimentoContabile) {
            cell_rKc4.setCellValue(this.getApplicationContext().getMessage("general.enabled", null, request.getLocale()));
          } else {
            cell_rKc4.setCellValue(this.getApplicationContext().getMessage("general.disabled", null, request.getLocale()));
          }
        }
        
        HSSFCell cell_rKc5 = row_k.createCell((short) 5);
        Boolean movimentoMagazzino = tipoDocumento_i.getMovimentoMagazzino();
        if (movimentoMagazzino != null) {
          if (movimentoMagazzino) {
            cell_rKc5.setCellValue(this.getApplicationContext().getMessage("general.enabled", null, request.getLocale()));
          } else {
            cell_rKc5.setCellValue(this.getApplicationContext().getMessage("general.disabled", null, request.getLocale()));
          }
        }
          
      }
    
    }
    
  }
  
}

